﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///SubType 的摘要说明
/// </summary>
public partial class SubType
{
    DataTableDataContext db = new DataTableDataContext();
    BookInfo bookInfoName = new BookInfo();
    //根据小类型名ID查找小类型
    public IEnumerable<SubType> GetSubTypeNameBySubId(int id)
    {
        var subTypes = from s in db.SubType where s.subTypeId == id select s;
        return subTypes;
    }

    
    //根据大类型ID查找小类型
    public IEnumerable<SubType> GetSubTypeNameBySuperId(int id)
    {
        var subTypes = from s in db.SubType from t in db.SuperType where s.superTypeId == t.superTypeId && s.superTypeId==id select s;
        return subTypes;

    }


    //增加小类型信息
    public void InsertSubType(string subName,int superId)
    {
        SubType types = new SubType {subTypeName=subName,superTypeId=superId};
        db.SubType.InsertOnSubmit(types);
        db.SubmitChanges();
    }

    //增加小类型信息并返回ID
    public int InsertSubTypeReturnId(string subName, int superId)
    {
        SubType types = new SubType { subTypeName = subName, superTypeId = superId };
        db.SubType.InsertOnSubmit(types);
        db.SubmitChanges();
        int id = types.subTypeId;
        return id;
    }

    //删除小类型信息
    public void DeleteSubType(int id)
    {
        var subTypes = from s in db.SubType where s.subTypeId == id select s;
        var books = from b in db.BookInfo from s in db.SubType where b.bookTypeId == s.subTypeId && s.subTypeId==id select b;
        foreach (var book in books)
        {
            bookInfoName.deleteBook(book.bookId);
        }
        foreach (var subType in subTypes)
        {
            db.SubType.DeleteOnSubmit(subType);
            db.SubmitChanges();
        }
        
    }



}